<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="楼盘名称id" prop="outsideId">
        <el-input
          v-model="queryParams.outsideId"
          placeholder="楼盘名称id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼盘名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入楼盘名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商圈" prop="commercialArea">
        <el-input
          v-model="queryParams.commercialArea"
          placeholder="请输入商圈"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="附近地铁" prop="subway">
        <el-input
          v-model="queryParams.subway"
          placeholder="请输入附近地铁"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建筑面积" prop="proportion">
        <el-input
          v-model="queryParams.proportion"
          placeholder="请输入建筑面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼栋数" prop="buildingCount">
        <el-input
          v-model="queryParams.buildingCount"
          placeholder="请输入楼栋数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开发商" prop="developer">
        <el-input
          v-model="queryParams.developer"
          placeholder="请输入开发商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物业管理公司" prop="estate">
        <el-input
          v-model="queryParams.estate"
          placeholder="请输入物业管理公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ruoyi-premises:premises:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyi-premises:premises:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyi-premises:premises:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ruoyi-premises:premises:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="goUpdateStatus()"
        >文件上传</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="premisesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="楼盘名称id" align="center" prop="outsideId" />
      <el-table-column label="楼盘名称" align="center" prop="name" />
      <el-table-column label="商圈" align="center" prop="commercialArea" />
      <el-table-column label="附近地铁" align="center" prop="subway" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="建筑面积" align="center" prop="proportion" />
      <el-table-column label="楼栋数" align="center" prop="buildingCount" />
      <el-table-column label="开发商" align="center" prop="developer" />
      <el-table-column label="物业管理公司" align="center" prop="estate" />
      <el-table-column label="楼盘简介" align="center" prop="introduce" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyi-premises:premises:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi-premises:premises:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改楼盘信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="楼盘名称id" prop="outsideId">
          <el-input v-model="form.outsideId" placeholder="请输入楼盘名称id" />
        </el-form-item>
        <el-form-item label="楼盘名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入楼盘名称" />
        </el-form-item>
        <el-form-item label="商圈" prop="commercialArea">
          <el-input v-model="form.commercialArea" placeholder="请输入商圈" />
        </el-form-item>
        <el-form-item label="附近地铁" prop="subway">
          <el-input v-model="form.subway" placeholder="请输入附近地铁" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="建筑面积" prop="proportion">
          <el-input v-model="form.proportion" placeholder="请输入建筑面积" />
        </el-form-item>
        <el-form-item label="楼栋数" prop="buildingCount">
          <el-input v-model="form.buildingCount" placeholder="请输入楼栋数" />
        </el-form-item>
        <el-form-item label="开发商" prop="developer">
          <el-input v-model="form.developer" placeholder="请输入开发商" />
        </el-form-item>
        <el-form-item label="物业管理公司" prop="estate">
          <el-input v-model="form.estate" placeholder="请输入物业管理公司" />
        </el-form-item>
        <el-form-item label="楼盘简介" prop="introduce">
          <el-input v-model="form.introduce" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-form-item label="七牛云上传文件" prop="title" :label-width="formLabelWidth">
          <el-upload
            drag
            class="avatar-uploader"
            action="http://localhost:9205/qiniukodo/upload"
            :show-file-list="false"
            :before-upload="handleUploadPreview"
            :on-success="handleUploadSuccess"
            :on-error="handleUploadError">
            <img style="width: 100px; height: 100px" v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="大文件上传" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="大文件上传">
          <uploader :options="options" class="uploader-example" @file-added="onFileAdded">
            <uploader-unsupport></uploader-unsupport>
            <uploader-drop>
              <p>Drop files to upload or</p>
              <uploader-btn>select files</uploader-btn>
              <!--        <uploader-btn :attrs="attrs">select iimages</uploader-btn>
                      <uploader-btn :directory="true">select folder</uploader-btn>-->
            </uploader-drop>
            <uploader-list></uploader-list>
          </uploader>
        </el-form-item>
      </el-form>>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPremises, getPremises, delPremises, addPremises, updatePremises } from "@/api/premises/premises";
import SparkMD5 from 'spark-md5'
export default {
  name: "Premises",
  data() {
    return {
      from:{},
      options: {
        target: "http://localhost:9205/qiniukodo/fileUpload", // 目标上传 URL
        chunkSize: '2048000',   //分块大小
        fileParameterName: 'file', //上传文件时文件的参数名，默认file
        maxChunkRetries: 3,  //最大自动失败重试上传次数
        testChunks: false,     //是否开启服务器分片校验
      },
      attrs:{
        accept:'image/*'
      },
      dialogFormVisible: false,
      imageUrl: '',
      formLabelWidth:'120px',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 楼盘信息表格数据
      premisesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        outsideId: null,
        name: null,
        province: null,
        city: null,
        county: null,
        commercialArea: null,
        subway: null,
        address: null,
        proportion: null,
        grossPlotRatio: null,
        greenCoverage: null,
        buildingCount: null,
        efficiency: null,
        developer: null,
        estate: null,
        introduce: null,
        communityType: null,
        buildingType: null,
        premisesType: null,
        longItude: null,
        latItude: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    onFileAdded (file) {
      const fileReader = new FileReader()
      fileReader.readAsBinaryString(file)
      fileReader.onload = e => {
        const md5 = SparkMD5.hashBinary(e.target.result)
        console.log(md5)
        file.uniqueIdentifier = md5
      }
    },
    goUpdateStatus(){
      this.dialogFormVisible=true
    },
    handleUploadPreview(file) {//文件上传进行大小判断
      console.log(file);
      const isLt2M = file.size / 1024 / 1024 < 25;
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过 25MB!');
      }
      return isLt2M;
    },
    handleUploadSuccess(res, file) {//处理成功后的设置
      if (res.code === 200) {
        this.imageUrl = URL.createObjectURL(file.raw);
        this.$message.success('上传成功')
      } else {
        this.$message.success(res.msg)
      }
    },
    handleUploadError(err, file) {
      this.$message.error("失败" + err)
    },
    /** 查询楼盘信息列表 */
    getList() {
      this.loading = true;
      listPremises(this.queryParams).then(response => {
        this.premisesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        outsideId: null,
        name: null,
        province: null,
        city: null,
        county: null,
        commercialArea: null,
        subway: null,
        address: null,
        proportion: null,
        grossPlotRatio: null,
        greenCoverage: null,
        buildingCount: null,
        efficiency: null,
        developer: null,
        estate: null,
        introduce: null,
        communityType: null,
        buildingType: null,
        premisesType: null,
        updateTime: null,
        longItude: null,
        latItude: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.imageUrl="";
      this.open = true;
      this.title = "添加楼盘信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.imageUrl="";
      const id = row.id || this.ids
      getPremises(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改楼盘信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePremises(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPremises(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除楼盘信息编号为"' + ids + '"的数据项？').then(function() {
        return delPremises(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ruoyi-premises/premises/export', {
        ...this.queryParams
      }, `premises_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.uploader-example {
  width: 480px;
  padding: 15px;
  margin: 40px auto 0;
  font-size: 12px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 4);
}
.uploader-example .uploader-btn {
  margin-right: 4px;
}
.uploader-example .uploader-list {
  max-height: 440px;
  overflow: auto;
  overflow-x: hidden;
  overflow-y: auto;
}
</style>
